<template>
  <div>
    <van-nav-bar title="乘车码" left-text="返回" left-arrow @click-left="back" />
    <div v-if="!flage">
        <van-form @submit="onSubmit" >
  <van-field
    v-model="idcard"
    name="idcard"
    label="身份证号码"
    placeholder="身份证号码"
    :rules="[{ required: true, message: '请填写身份证号码' }]"
  />
  <van-field
    v-model="phone"
    name="phone"
    label="手机号"
    placeholder="手机号"
    :rules="[{ required: true, message: '请填写手机号' }]"
  />
  <van-field
    v-model="name"
    name="name"
    label="用户名"
    placeholder="用户名"
    :rules="[{ required: true, message: '请填写用户名' }]"
  />
  <div style="margin: 16px;">
    <van-button round block type="info" native-type="submit">领取乘车码</van-button>    
  </div>
</van-form>
    </div>


    <div v-if="flage" class="info">
        <h3>我的乘车码信息</h3>
        <p>用户名:{{info.userName}}</p>
        <p>用户id:{{info.userId}}</p>
        <p>身份证号:{{info.idCard}}</p>
        <p>联系电话:{{info.phonenumber}}</p>
        <p>乘车码卡号:{{info.cardNum}}</p>
        <div style="margin-top: 26px">
        <van-button round block type="info" native-type="submit" @click="del">删除乘车码</van-button>
    </div>
    </div>

  </div>
</template>

<script>
import {cardget,cardadd,carddel} from "./Service"
import mixin from  "@/mixin/flag";
export default {
    mixins:[mixin],
    data(){
        return {
            name:'',
            phone:'',
            idcard:'',
            info:'',
            flage:'',
        }
    },
    methods:{

        back(){
            this.$router.go(-1);
        },
        async del(){
            let res = await carddel({
                id:this.info.id
            })
            console.log(res);
            if(res.data.code ==200){ //表示成功
                this.$toast.success(res.data.msg);
                window.location.reload()
            }
        },
        async onSubmit(){
            let res= await cardadd({
                idCard:this.idcard,
                phonenumber:this.phone,
                userName:this.name
            })
            console.log(res);
            if(res.data.code ==200){ //表示成功
                this.$toast.success(res.data.msg);
                window.location.reload()
                // let res1 = await cardget()
                // this.info = res1.data.data
            }

        }
    },
    async created(){
        let res = await cardget();
        this.info = res.data.data,
        console.log(res);
        if(res.data.code !==200){ //表示失败
            this.$toast.fail(res.data.msg);
            this.flage=false
        }else{
            this.flage=true
        }
    }
}
</script>

<style lang="scss" scoped>
.info{
    h3{
        margin: 50px 10px 20px 10px;
    }
    p{
        font-size: 14px;
        margin-left: 10px;
        line-height: 26px;
    }
}

</style>